const path = require("path");
const fs = require("fs");

const ejs = require("ejs");

const compile = (templateName, data) => {
  // 拼接字符串
  const templatePosition = `../templates/${templateName}`;
  const templetePath = path.resolve(__dirname, templatePosition);

  // console.log(templetePath);

  return new Promise((resolve, reject) => {
    ejs.renderFile(templetePath, { data }, {}, (err, data) => {
      if (err) {
        reject(err);
        throw err;
      }
      resolve(data);
    });
  });
};

const writeToFile = (path, content) => {
  return fs.promises.writeFile(path, content);
};

const createDirSync = (pathName) => {
  if (fs.existsSync(pathName)) {
    return true
  }

  if (createDirSync(path.dirname(pathName))) {
    fs.mkdirSync(pathName)
    return true
  }
}



module.exports = {
  compile,
  writeToFile,createDirSync
};
